package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.pas04.Pas04ResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <pre>
 * 分析任务
 * Created by clb on 17-3-17.
 * </pre>
 */
@Api(description = "分析结果")
@RestController
@RequestMapping("")
public class ResultController {
    @Autowired
    private Pas04ResultService pas04ResultService;

    @ApiOperation(value = "作业文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hdfsPath", value = "hdfs文件路径",  required = true,dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "expand", value = "是否查询子文件", dataType = "Boolean", paramType = "query")
    })
    @GetMapping(value = "/files")
    public ResponseEntity<CommonResultResponse> files(@RequestParam("hdfsPath") String hdfsPath,
                                                    @RequestParam(value = "expand", required = false) Boolean expand) throws Exception {
        if(expand == null){
            expand = false;
        }
        return ResponseHelper.success(pas04ResultService.getFilesByPath(hdfsPath,expand));
    }

    @ApiOperation(value = "作业文件的下载路径")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hdfsPath", value = "hdfs文件路径",  required = true,dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fileName", value = "文件名称",required = false, dataType = "String", paramType = "query")
    })
    @GetMapping(value = "/files/downloadUrl")
    public ResponseEntity<CommonResultResponse> downloadUrl(@RequestParam("hdfsPath") String hdfsPath,
                                                      @RequestParam(value = "fileName", required = false) String fileName) throws Exception {
        return ResponseHelper.success(pas04ResultService.getFileDownLoadUrl(hdfsPath,fileName));
    }

    @ApiOperation(value = "接口数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "jobId", value = "作业id", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "projectId", value = "项目编号", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "deviceNo", value = "设备编号", required = false, dataType = "String", paramType = "query")
    })
    @GetMapping(value = "/interfaces")
    public ResponseEntity<CommonResultResponse> getInterfaces(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                              @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                              @RequestParam(value = "jobId" ,required = false) Long jobId,
                                                              @RequestParam(value = "projectId",required = false) Long projectId,
                                                              @RequestParam(value = "deviceNo",required = false) String deviceNo) throws Exception {
        List list = pas04ResultService.getInterfaces(projectId,jobId,deviceNo,null,null);
        pageNum = (pageNum == null) ? Integer.valueOf(1) : pageNum;
        pageSize = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        int count = list.size();
        PageInfo pageInfo = new PageInfo(pageNum, pageSize, count);
        return ResponseHelper.success(list,pageInfo);
    }

    @ApiOperation(value = "获取结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobId", value = "作业编号", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "结果类型", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping(value = "/results")
    public ResponseEntity<CommonResultResponse> results(@RequestParam("jobId") Long jobId,
                                                                    @RequestParam String type) throws Exception {
        return ResponseHelper.success(pas04ResultService.results(jobId,type));
    }
}
